package com.example.project4;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button loginButton;
    private Button registerButton;
    private ProgressBar loadingProgressBar;
    private TextView loadingTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化视图组件
        loginButton = findViewById(R.id.loginButton);
        registerButton = findViewById(R.id.registerButton);
        loadingProgressBar = findViewById(R.id.loadingProgressBar);
        loadingTextView = findViewById(R.id.loadingTextView);

        // 设置登录按钮的点击监听器
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startLoadingProcess();
            }
        });

        // 设置注册按钮的点击监听器
        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 注册按钮也触发相同的加载流程
                startLoadingProcess();
            }
        });
    }

    private void startLoadingProcess() {
        // 1. 立即显示圆形进度条和提示文本
        loadingProgressBar.setVisibility(View.VISIBLE);
        loadingTextView.setVisibility(View.VISIBLE);

        // 2. 使用 Handler 延迟3秒执行后续操作
        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                // 3. 3秒后，隐藏进度条和提示文本
                loadingProgressBar.setVisibility(View.GONE);
                loadingTextView.setVisibility(View.GONE);

                // 4. 同时弹出“登录成功”提示
                Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
            }
        }, 3000); // 3000毫秒 = 3秒
    }
}